<?php
include_once 'class/LogReader.aclass.inc';
class FileLogReader extends LogReader {
	private $path = '';
	private $files = array();
	private $currentFileHandler = null;
	private $filesArrayPointer = 0;
	private $currentFileOffset = 0;
	private $listeners=array();
	
	function __construct($filename = false, $path = '') {
		$this->setPath($path);
		$this->addFile($filename);
		$this->reset();
	}
	
	//optimize - check for Listener Class Type
	public function addListener($listener){
		$this->listeners[]=$listener;
		$all =count($this->files);
		$done = $this->filesArrayPointer;
		$listener->onFileUpdate($all, $done);		
	}
	
	public function fireOnFileUpdate($fake_done = false) {
		
		$all =count($this->files);
		$done = $this->filesArrayPointer + 1;
		if(is_int($fake_done))
			$done = $fake_done;
		
	foreach ($this->listeners as $list) {
		$list->onFileUpdate($all, $done);
	};
	}
	public function addFile($filename, $unique = true) {
		if(is_array($filename))
			foreach ($filename as $fname)
				$this->insertFile($fname, $unique);
		else 
			$this->insertFile($filename, $unique);
	}
	private function insertFile($filename, $unique = true) {
		$a = $unique == false;
		$b = false === array_search($this->path . $filename, $this->files);
		$c = array_search($this->path . $filename, $this->files);
		if (is_string($filename))
			if ($unique == false || false === array_search($this->path . $filename, $this->files))	// test the file unique if need
				$this->files[] = $this->path . $filename;
	}
	public function setPath($path) {
		if (is_string($path) && $path != '') {
			$this->path = $path.'/';
		}
	}
	private function nextFile() {
		$this->fireOnFileUpdate();
		if (is_resource($this->currentFileHandler))
			fclose($this->currentFileHandler);
		if (isset($this->files[$this->filesArrayPointer+1])) {
			$this->filesArrayPointer++;
			$this->currentFileHandler = $this->getCurrentFileHandler();
			/*
			if ($this->currentFileHandler == false) {
				return false;
			}
			*/
			
			$this->currentFileOffset = 0;
			return $this->currentFileHandler;
		}
		return false;
	}
	public function reset(){
		if (is_resource($this->currentFileHandler))
			fclose($this->currentFileHandler);
		$this->currentFileOffset = 0;
		$this->filesArrayPointer = 0;
		$this->fireOnFileUpdate();
	}
	private function getCurrentFileHandler() {
		if(is_resource($this->currentFileHandler))
			return $this->currentFileHandler;
		elseif (isset($this->files[$this->filesArrayPointer]))
			return $this->currentFileHandler = @fopen($this->files[$this->filesArrayPointer], 'rb');
		return false;
	}
	public function read() {
		$handle = $this->getCurrentFileHandler();
		if($handle === false)
			return false;
		if (feof($handle) && !($handle = $this->nextFile())) {
			return false;
		}
		fseek($handle, $this->currentFileOffset, SEEK_SET);
		$str = fgets($handle);
		if ($str === false) {
			$str = '';
		}
		$this->currentFileOffset = ftell($handle);
		return $str;
	}
}
?>